<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;

class UsersController extends Controller
{
    //增
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:users|max:50',
            'mobile' => 'required|unique:users|max:11',
            'password' => 'required|confirmed|min:6'
        ]);

        try {
            DB::beginTransaction();
            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'mobile' => $request->mobile,
                'remarks' => $request->remarks,
            ]);
            $user->enterprises()->sync($request->input('enterprise_id'));
            $user->roles()->sync($request->input('role_id'));
    
            $user->load('enterprises');
            $user->load('roles');
            DB::commit();
            return $this->respondSuccess($user);
        } catch (QueryException $e) {
            DB::rollBack();
            return $this->respondError($e->getMessage());
        }
    }
    
    //删
    public function destroy(User $user)
    {
        $data = $user->delete();
        return $this->respondSuccess($data);
    }

    //改
    public function update(Request $request, User $user)
    {
        $this->validate($request, [
            'name' => 'nullable|max:50',
            'password' => 'nullable|confirmed|min:6'
        ]);
        
        try {
            DB::beginTransaction();
            $data = $request->only([
                'name',
                'email',
                'password',
                'mobile',
                'remarks',
            ]);;
            if ($request->password) {
                $data['password'] = bcrypt($request->password);
            }
            $user->update($data);
            
            if ($request->has('enterprise_id'))
                $user->enterprises()->sync($request->input('enterprise_id'));
            if ($request->has('role_id'))
                $user->roles()->sync($request->input('role_id'));
    
            $user->load('enterprises');
            $user->load('roles');
            DB::commit();
            return $this->respondSuccess($user);
        } catch (QueryException $e) {
            DB::rollBack();
            return $this->respondError($e->getMessage());
        }
    }

    //查
    public function index(Request $request)
    {
        $users = User::with(['enterprises','roles'])->requestCriteria($request)->paginate($request->query('limit'));
        return $this->respondSuccess($users);
    }

    //详
    public function show(User $user)
    {
        $user->load(['enterprises','roles']);
        return $this->respondSuccess($user);
    }

    //详查
    public function show_info(User $user, $info)
    {
        if($info){
            return $this->respondSuccess($user->$info()->get());
        }
    }

    public function selfUpdate(Request $request)
    {
        $user = $request->user();
        $this->validate($request, [
            'name' => 'nullable|max:50',
            'password' => 'nullable|confirmed|min:6'
        ]);
        
        try {
            $data = $request->only([
                'name',
                'email',
                'password',
                'mobile',
                'remarks',
            ]);
            if ($request->password) {
                $data['password'] = bcrypt($request->password);
            }
            $user->update($data);

            return $this->respondSuccess($user);
        } catch (QueryException $e) {
            return $this->respondError($e->getMessage());
        }
    }

}
